package com.jing.shop.controller;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jing.common.core.domain.R;
import com.jing.shop.domain.dto.ProductAttributeCategoryDTO;
import com.jing.shop.domain.vo.PmsProductAttrRelVO;
import com.jing.shop.domain.vo.ProductAttributeCategoryVO;
import com.jing.shop.domain.vo.ProductShopCategoryVO;
import com.jing.shop.service.ProductAttributeCategoryService;
import com.jing.shop.service.ProductShopCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

import static com.jing.shop.controller.Test.cartesianProductIterative;

@Api(tags = "商品类型管理")
@RestController()
@RequestMapping("/attrCateGory")
public class ProductAttributeCategoryController{

    private static final Logger log = LoggerFactory.getLogger(ProductAttributeCategoryController.class);

    @Autowired
    private ProductAttributeCategoryService attributeCategoryService;

    @ApiOperation("商品类型列表")
    @PostMapping("/page")
    public R findAttribute(@RequestBody ProductAttributeCategoryDTO productAttributeCategoryDTO){
        PageInfo<ProductAttributeCategoryVO> attributeCategoryList = attributeCategoryService.findAttributeCategoryList(productAttributeCategoryDTO);
        return R.ok(attributeCategoryList,"查询成功");
    }

    @ApiOperation("添加商品类型分类")
    @PostMapping("/add")
    public R addCategory(@Valid @RequestBody ProductAttributeCategoryDTO categoryDTO){
        attributeCategoryService.addAttributeCategory(categoryDTO);
        return R.ok("添加成功");
    }

    @ApiOperation("修改商品类型分类")
    @PostMapping("/update")
    public R updateCategory(@Valid @RequestBody ProductAttributeCategoryDTO categoryDTO){
        attributeCategoryService.updateAttributeCategory(categoryDTO);
        return R.ok("修改成功");
    }

    @ApiOperation("删除商品类型分类")
    @PostMapping("/del/{categoryId}")
    public R delCategory(@Valid @PathVariable Integer categoryId){
        attributeCategoryService.delAttributeCategory(categoryId);
        return R.ok("删除成功");
    }

    @Autowired
    private ProductShopCategoryService productShopCategoryService;

    @ApiOperation("属性类型下拉框")
    @GetMapping("/findCategory")
    public R findCategory(){
        List<ProductAttributeCategoryVO> category = productShopCategoryService.findCategory();
        return R.ok(category);
    }

    @ApiOperation("商品规格")
    @GetMapping("/findByCid")
    public R findByCid(@RequestParam Integer cid,@RequestParam Integer type){
        List<ProductShopCategoryVO> byCid = productShopCategoryService.findByCid(cid, type);
        return R.ok(byCid);
    }

    @PostMapping("/changeAttrRelToSku")
    @ApiOperation(value = "商品Sku格式转换")
    public R<List<Map<Long,String>>> changeAttrRelToSku(@RequestBody List<PmsProductAttrRelVO> pmsProductAttrRelVO){
        return R.ok(productShopCategoryService.changeAttrRelToSku(pmsProductAttrRelVO));
    }

    @PostMapping("/changeAttrToSku")
    public R<List<Map<String, String>>> attrList(@RequestBody List<PmsProductAttrRelVO> pmsProductAttrRelVOList) {
        // 分组
        List<PmsProductAttrRelVO> relVos=pmsProductAttrRelVOList;
        Map<String, List<PmsProductAttrRelVO>> groupedByCid = relVos.stream()
                .collect(Collectors.groupingBy(
                        map -> String.valueOf(map.getCid()), // 分组键
                        Collectors.toList() // 收集器
                ));
        //TODO  转换
        // 假设我们已经有了一个包含所有不同cid的Set
        Set<Integer> uniqueCids = new HashSet<>(groupedByCid.keySet().stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList()));

        // 获取所有可能的组合
        List<Map<String, String>> combinations = new ArrayList<>();
        List<List<String>> cidValues = uniqueCids.stream()
                .map(cid -> groupedByCid.getOrDefault(String.valueOf(cid), Collections.emptyList())
                        .stream()
                        .map(PmsProductAttrRelVO::getAttrName)
                        .collect(Collectors.toList()))
                .collect(Collectors.toList());

        // 使用笛卡尔积生成所有组合
        cartesianProductIterative(cidValues).forEach(combination -> {
            Map<String, String> result = new LinkedHashMap<>();
            for (int i = 0; i < combination.size(); i++) {
                result.put(String.valueOf(uniqueCids.toArray(new Integer[0])[i]), combination.get(i));
            }
            combinations.add(result);
        });
        // 输出结果
        System.out.println(JSONObject.toJSONString(combinations));
        return R.ok(combinations);

    }

}
